/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tracker.stub;

import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import tracker.entity.UsersEntity;
import tracker.entity.UsersEntityFacade;

/**
 *Workshop 
 *Class ID        : SE0691   
 *Student ID      : SE90009
 *Student Name    : Phạm Nguyễn Nhật Trung
 *Due Date        : 12 November 2012
 *I declare that this assignment is my own work
 *in accordance with FPT Policy.
 */
public class UsersSessionStub {
    UsersEntityFacade usersEntityFacade = lookupUsersEntityFacadeBean();

    public void insert(UsersEntity entity){
        usersEntityFacade.create(entity);
    }
    public UsersEntity findUserById(int userId){
        return usersEntityFacade.find(userId);
    }
    public void deleteById(int userId){
        UsersEntity entity = findUserById(userId);
        usersEntityFacade.remove(entity);
    }
    public void change(UsersEntity entity){
        usersEntityFacade.edit(entity);
    }
    public List<UsersEntity> findAll(){
        return usersEntityFacade.findAll();
    }
    
    public Collection getAllUsers(){
        Collection<UsersEntity> list = null;
        return usersEntityFacade.findAll();
    }
    public boolean checkLogin(String username, String password){
        return usersEntityFacade.checkLogin(username, password);
    }
    
    public boolean checkIsExistedUsername(String username){
        return usersEntityFacade.checkIsExistedUsername(username);
    }
    
    public int checkLoginReturnUserId(String userName, String password){
        return usersEntityFacade.checkLoginReturnUserId(userName, password);
    }
    
    public List findAllUsersByUserName(String userName){
        return usersEntityFacade.findAllUsersByUserName(userName.trim());
    }
    
    private UsersEntityFacade lookupUsersEntityFacadeBean() {
        try {
            Context c = new InitialContext();
            return (UsersEntityFacade) c.lookup("java:global/ASG2-Tracker1/ASG2-Tracker1-ejb/UsersEntityFacade!tracker.entity.UsersEntityFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
